package com.surelution.projects.crm
import org.codehaus.groovy.grails.commons.ConfigurationHolder


class CompetitorCompanyController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def pictureService
	
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [competitorCompanyInstanceList: CompetitorCompany.list(params), competitorCompanyInstanceTotal: CompetitorCompany.count()]
    }

    def create = {
        def competitorCompanyInstance = new CompetitorCompany()
        competitorCompanyInstance.properties = params
        return [competitorCompanyInstance: competitorCompanyInstance]
    }

    def save = {
        def competitorCompanyInstance
		def title = params.name
		def file = params.file
		def description = "logoIcon"
		if(!file.isEmpty()) {
			Picture picture = pictureService.storePicture(file.inputStream)
			picture.title = title
			picture.description = description
			picture.uploadTime = new Date()
			picture.save(flush:true)
			competitorCompanyInstance = new CompetitorCompany()
			competitorCompanyInstance.name = title
			competitorCompanyInstance.logoIcon = picture
			competitorCompanyInstance.address = params.address
			competitorCompanyInstance.tel = params.tel
			try{
				competitorCompanyInstance.save(flush:true)
			}catch(e) {
				e.printStackTrace()
			}
	        flash.message = "${message(code: 'default.created.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), competitorCompanyInstance.id])}"
	        redirect(action: "show", id: competitorCompanyInstance.id)
		}
        else {
            render(view: "create", model: [competitorCompanyInstance: competitorCompanyInstance])
        }
    }

    def show = {
        def competitorCompanyInstance = CompetitorCompany.get(params.id)
        if (!competitorCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            [competitorCompanyInstance: competitorCompanyInstance]
        }
    }

    def edit = {
        def competitorCompanyInstance = CompetitorCompany.get(params.id)
        if (!competitorCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [competitorCompanyInstance: competitorCompanyInstance]
        }
    }

    def update = {
        def competitorCompanyInstance = CompetitorCompany.get(params.id)
        if (competitorCompanyInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (competitorCompanyInstance.version > version) {
                    
                    competitorCompanyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'competitorCompany.label', default: 'CompetitorCompany')] as Object[], "Another user has updated this CompetitorCompany while you were editing")
                    render(view: "edit", model: [competitorCompanyInstance: competitorCompanyInstance])
                    return
                }
            }
            competitorCompanyInstance.properties = params
            if (!competitorCompanyInstance.hasErrors() && competitorCompanyInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), competitorCompanyInstance.id])}"

				if (!competitorCompanyInstance.hasErrors() && competitorCompanyInstance.save(flush: true)) {
					def file = params.file
					if(!file.isEmpty()) {
						Picture picture = pictureService.storePicture(file.inputStream)
						picture.title = params.name
						picture.description = "Competitor"
						picture.save(flush:true)
						competitorCompanyInstance.logoIcon = picture
						try{
							competitorCompanyInstance.save(flush:true)
						}catch(e) {
							e.printStackTrace()
						}
					}
				}
                redirect(action: "show", id: competitorCompanyInstance.id)
            }
            else {
                render(view: "edit", model: [competitorCompanyInstance: competitorCompanyInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def competitorCompanyInstance = CompetitorCompany.get(params.id)
        if (competitorCompanyInstance) {
            try {
                competitorCompanyInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'competitorCompany.label', default: 'CompetitorCompany'), params.id])}"
            redirect(action: "list")
        }
    }

}
